package akka.scala.liuwei

import akka.actor.{Actor, ActorSystem, Props}

/**
  * 定义 AkkaTest1 Actor
  * Created by liuwei on 2017/5/11.
  */
class AkkaTest1 extends Actor {
  /**
    * receive方法定义实现，其中使用了模式匹配表达式
    * 接收字符串Hello作为消息，做出相应打印动作
    * @return
    */
  override def receive: Receive = {
    case "Hello" => println("world")
    case _ => println("no world")
  }
}

/**
  * 测试actor
  */
object Test1_AkkaTest1 extends App {
  //初始化一个actor系统
  //ActorSystem接收一个name参数，并且通过system.actorOf创建actor实例
  val system = ActorSystem("Test1_AkkaTest1")
  //初始化一个actor,Actor创建后自动运行
  val helloWorldActor = system.actorOf(Props[AkkaTest1], name = "helloWorld")
  //通过!方法来发送消息
  helloWorldActor ! "Hello"
  helloWorldActor ! "liuwei"
  helloWorldActor ! "Hello liuwei"
  // g关闭系统
  system.terminate()
}